package com.hunktimes.wholesale.widget

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.text.SpannableString
import android.text.method.LinkMovementMethod
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.core.content.ContextCompat
import com.hunktimes.wholesale.R
import com.hunktimes.wholesale.util.DisplayUtil
import com.hunktimes.wholesale.util.L

/**
 * Author: Delete
 * Date: 2020/7/29 17:15
 * Description:
 *
 */
class ProtocolDialog:Dialog {

    companion object {
        val default_layoutRes = R.layout.dialog_protocol
    }

    var message: String? = null
    var negativeButtonMsg: String? = null
    var positiveButtonMsg: String? = null
    var positionListener: DialogInterface.OnClickListener? = null
    var negativeListener: DialogInterface.OnClickListener? = null
    @LayoutRes
    private var layotuResId: Int? = null
    var contentAction: ContentAction? = null

    constructor(builder: Builder) : super(builder.context, R.style.WholeSaleDialogTheme) {
        this.message = builder.message
        this.negativeButtonMsg = builder.negativeButtonMsg
        this.positiveButtonMsg = builder.positiveButtonMsg
        this.positionListener = builder.positiveListener
        this.negativeListener = builder.negativeListener
        this.layotuResId = builder.layoutResId
        this.contentAction = builder.contentAction
        init()
    }

    fun init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        val contentView = LayoutInflater.from(context)
            .inflate(layotuResId ?: default_layoutRes, null, false)
        if (layotuResId != default_layoutRes)
            contentAction?.onDialogViewCreated(this, contentView)
        val contentTxt = context.getString(R.string.protocol_text)
        val origin = SpannableString(contentTxt)
        val start1 = contentTxt.indexOf("《")
        val end1 = contentTxt.indexOf("》")
        val color = ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimary))
        origin.setSpan(color, start1, end1, SpannableString.SPAN_INCLUSIVE_INCLUSIVE)
        val protocalClick1 = ProtocolClickableSpan()
        origin.setSpan(protocalClick1, start1, end1, SpannableString.SPAN_INCLUSIVE_INCLUSIVE)

        val protocolTv = contentView.findViewById<TextView>(R.id.protocol_tv)
        protocolTv.movementMethod = LinkMovementMethod.getInstance()
        protocolTv.highlightColor = ContextCompat.getColor(context, android.R.color.transparent)
        protocolTv.text = origin
        setContentView(contentView)
        setCancelable(false)

        val cancelBtn = contentView.findViewById<TextView>(R.id.cancel_tv)
        val confirmBtn = contentView.findViewById<TextView>(R.id.confirm_tv)
        cancelBtn.text = negativeButtonMsg ?: "取消"
        confirmBtn.text = positiveButtonMsg ?: "确定"
        negativeListener?.let { negativeListener ->
            cancelBtn.setOnClickListener {
                negativeListener.onClick(this, cancelBtn.id)
            }
        }
        positionListener?.let { positionListener ->
            confirmBtn.setOnClickListener {
                positionListener.onClick(this, confirmBtn.id)
            }
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val window = window
        val lp = window!!.attributes
        //使用wrap login dialog 会存在 很窄的情况
//        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        val screenWidth = DisplayUtil.getScreenWidth(context)
        lp.width = (screenWidth * 0.72f).toInt()
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
//        lp.height = (DisplayUtil.getScreenWidth(context) * 0.34f).toInt()
        lp.dimAmount = 0.3f//背景透明度 0全透 1 全黑
        window.attributes = lp
    }
    class Builder{
        var message: String? = null
        var negativeButtonMsg: String? = null
        var positiveButtonMsg: String? = null
        var positiveListener: DialogInterface.OnClickListener? = null
        var negativeListener: DialogInterface.OnClickListener? = null
        @LayoutRes
        var layoutResId: Int = default_layoutRes

        var contentAction: ContentAction? = null

        var context: Context

        constructor(context: Context) {
            this.context = context
        }

        fun build(): ProtocolDialog = create()

        private fun create() = ProtocolDialog(this)
    }

    interface ContentAction {
        fun onDialogViewCreated(dialog: Dialog, view: View)
    }
}